/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package scm;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
 *
 * @author Łukasz Synówka
 */
public class ModyfikowanieProcedury extends javax.swing.JFrame {

    private Connection c = null;
    /** Creates new form ModyfinowanieProcedury */
    public ModyfikowanieProcedury() {
        initComponents();
        wypelnij();
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        jComboBox1.setName("jComboBox1"); // NOI18N


        jComboBox1.addActionListener(new ActionListener()
        {

	    public void actionPerformed(ActionEvent e)
            {
		 JComboBox jcb = (JComboBox) e.getSource();
	       	 String procedure = (String) jcb.getSelectedItem();
                 String[] parts = procedure.split("\\.");
                 jComboBox3.removeAllItems();
                 try
                 {
                     Class.forName("org.postgresql.Driver");
                 }
                 catch (ClassNotFoundException cnfe)
                 {
                     JOptionPane.showMessageDialog(null, cnfe.getMessage());
                 }
                 try
                 {
                     c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

                     PreparedStatement ps = c.prepareStatement("SELECT actions.id, actions.name FROM actions, proc_act WHERE " +
                                           "actions.id = proc_act.id_act AND proc_act.id_proc = ?");
                     ps.setInt(1, Integer.parseInt(parts[0]));
                     ResultSet rs = ps.executeQuery();


                     while (rs.next())
                     {
                         jComboBox3.addItem(rs.getString("id")+"."+rs.getString("name"));
                     }

                 }
                 catch (SQLException se)
                 {
                    JOptionPane.showMessageDialog(null, se.getMessage());
                 }

	    }
	});

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        jComboBox2.setName("jComboBox2"); // NOI18N

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));
        jComboBox3.setName("jComboBox3"); // NOI18N

        jButton1.setText("Ok"); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Anuluj"); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jLabel1.setText("Wybierz procedurę"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Dodaj instrukcję"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Usuń instrukcję"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N



        jButton4.setText("dodaj"); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });
        jButton5.setText("usuń"); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(17, 17, 17)
                                .addComponent(jButton2))
                            .addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 131, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            )
                        .addGap(21, 21, 21))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(117, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        this.dispose();
    }

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
        this.dispose();
    }

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt)
    {
        boolean jest = false;
        String procedure = (String) jComboBox1.getSelectedItem();
        String[] parts1 = procedure.split("\\.");
        
        String action = (String) jComboBox2.getSelectedItem();
        String[] parts2 = action.split("\\.");
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id FROM proc_act WHERE id_proc = ? AND id_act = ?");
            ps.setInt(1, Integer.parseInt(parts1[0]));
            ps.setInt(2, Integer.parseInt(parts2[0]));
            ResultSet rs = ps.executeQuery();
            while (rs.next())
            {
                 jest = true;
            }
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }


        if (jest)
        {
            JOptionPane.showMessageDialog(null, "Instrukcja jest już częścią procedury!");
        }
        else
        {
            jComboBox3.addItem(parts2[0] + "." + parts2[1]);
            try
            {
                Class.forName("org.postgresql.Driver");
            }
            catch (ClassNotFoundException cnfe)
            {
                JOptionPane.showMessageDialog(null, cnfe.getMessage());
            }
            try
            {
                c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

                PreparedStatement ps = c.prepareStatement("INSERT INTO proc_act(id_proc, id_act) VALUES(?, ?)");
                ps.setInt(1, Integer.parseInt(parts1[0]));
                ps.setInt(2, Integer.parseInt(parts2[0]));
                int rows_affected = ps.executeUpdate();
            }
            catch (SQLException se)
            {
                JOptionPane.showMessageDialog(null, se.getMessage());
            }
        }
    }
    private void jButton5MouseClicked(java.awt.event.MouseEvent evt)
    {
        String[] parts1 = jComboBox1.getSelectedItem().toString().split("\\.");
        String[] parts2 = jComboBox3.getSelectedItem().toString().split("\\.");

        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("DELETE FROM proc_act WHERE id_proc = ? AND id_act = ?");
            ps.setInt(1, Integer.parseInt(parts1[0]));
            ps.setInt(2, Integer.parseInt(parts2[0]));
            int rows_affected = ps.executeUpdate();
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }
        jComboBox3.removeItem(jComboBox3.getSelectedItem().toString());
    }

    public void wypelnij()
    {
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id, name FROM procedures");
            ResultSet rs = ps.executeQuery();
            while (rs.next())
            {
                 jComboBox1.addItem(rs.getString("id")+"."+rs.getString("name"));
            }
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }


        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id, name FROM actions");
            ResultSet rs = ps.executeQuery();
            while (rs.next())
            {
                 jComboBox2.addItem(rs.getString("id")+"."+rs.getString("name"));
            }
        }
        catch (SQLException se)
        {
            JOptionPane.showMessageDialog(null, se.getMessage());
        }
    }


    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration

}
